/*********************************************************************************************************
Class Name: LogDispositionExtension 
* Author: Accenture IDC
* Update Date: 16-May-2012  
* Description: Extension class for "Log Dispostion" page. Used for logging dispositions on Lead, Account,
*              and Opportunity via the Detail, and List View page button.
*********************************************************************************************************/

public with sharing class LogDispositionExtension
{
    public Notes__c CN{get;set;}//Creates an instance of the SObject Note
    public boolean leadtypeflag{get;set;}//Identifies if the disposition is for LEAD
    public boolean accounttypeflag{get;set;}//Identifies if the disposition is for ACCOUNT
    public boolean opptytypeflag{get;set;}//Identifies if the disposition is for OPPORTUNITY
    public boolean reasonflag{get;set;}//Identifies if the Disposition value = "Invalid" for recordtypes other than NMR
    public string Selecteddisp{get;set;}//Selected value from Disposition picklist
    public boolean recordtypeboolean{get;set;}//Identifies NMR Recordtypes for Disposition
    public boolean badreasonflag{get;set;}//Identifies if the Disposition value = "No Go - Bad Lead" for recordtypes of NMR
    public String logType{get;set;}//Identifies the Disposition from Lead, Account or Opportunity object
    public String logView{get;set;}//Identifies the Disposition from "Detail" or "ListView"
 
 /***
 Constructor to set the values from the disposition page
 ***/
    public LogDispositionExtension(ApexPages.StandardController controller){
        CN = (Notes__c)Controller.getRecord();
        CN.lead__c = ApexPages.currentPage().getParameters().get('leadid');
        logView = ApexPages.currentPage().getParameters().get('view');
        logType = ApexPages.currentPage().getParameters().get('type');  
        leadtypeflag = false;
        accounttypeflag = false;
        opptytypeflag = false;
        
        if(ApexPages.currentPage().getParameters().get('type') == 'Lead'){//For Disposition from Lead
            leadtypeflag = true;
            reasonflag=false;
            
            if(ApexPages.currentPage().getParameters().get('rectype') == 'NMR Lead' ||ApexPages.currentPage().getParameters().get('rectype') == 'NMR Lead with CustomerId'){      
                recordtypeboolean = true;
            }
            else{
                recordtypeboolean = false;
            }
        }
        else if(ApexPages.currentPage().getParameters().get('type') == 'accnt'){//For Disposition from Account
            accounttypeflag = true;
        }
        else{//For Disposition from Opportunity
            opptytypeflag = true;
        }
        
        CN.Account__c = ApexPages.currentPage().getParameters().get('accid');   
        CN.Opportunity__c = ApexPages.currentPage().getParameters().get('oppid');       
    }
/****
After user selects the values in disposition page, 
this method saves a note against the primary object record(Lead,Account or Opportunity)
****/
    public void saveNote()
    {   
        if(logType == 'Lead')
        {
            CN.Type__c = 'Lead';
            if(CN.Follow_Up__c == null){//If follow up date is NULL for current disposition, this picks the follow-up date from latest existing note of the lead
                List<notes__c> followUpNote = [select id, Follow_Up__c from notes__c where Follow_Up__c != null and lead__c =:ApexPages.currentPage().getParameters().get('leadid') order by createddate desc limit 1 ];
                if(followUpNote.size() > 0 )
                    CN.Follow_Up__c = followUpNote[0].Follow_Up__c;  
            }

            try{
                upsert CN;
               }
            catch(DmlException e){
                system.debug(e);ApexPages.addMessages(e);
               }
        }

        else if(logType == 'accnt')
        {
            CN.Type__c = 'Renewal';
            try{insert CN;}
            catch(DmlException e){system.debug(e);ApexPages.addMessages(e);}
        }
        else
        {
            CN.Type__c = 'Opportunity';
            try{insert CN;}
            catch(DmlException e){system.debug(e);ApexPages.addMessages(e);}
        }
        
    }
/***
This renders the reason field for different projects (ie NMR , OOR)
***/
    public pagereference RenderReason(){
        
        if(Selecteddisp == 'Invalid' && recordtypeboolean == false){
            reasonflag = true;
            badreasonflag = false;
        }else if(Selecteddisp == 'No Go - Bad Lead' && recordtypeboolean == true){
            badreasonflag = true;
            reasonflag = false;
        }
        else{
            reasonflag = false;
            badreasonflag = false;
        }
        return null;
    }
    
                
    //Test Method
    @isTest(SeeAllData=true)    
    static void logDisposistionTest() {

        Lead singleLead = new Lead(lastName = 'Test Lead',recordtypeid=RecordTypeHelper.GetRecordTypeID('Lead', 'NMR Lead'),phone='9992395645',Company='testcomp');        
        Account singleAccount = new Account(recordtypeid=RecordTypeHelper.GetRecordTypeID('Account', 'CSS Customer'), phone='8888395645', Name='Test Account');
        Opportunity singleOppty = new Opportunity(recordtypeid=RecordTypeHelper.GetRecordTypeID('Opportunity', 'NMR Opportunity'), Name='Test Oppty', StageName='Stage 1 - Proposal Created', CloseDate=System.today());
        
        test.startTest();
        insert singleLead;
        insert singleAccount;
        insert singleOppty;
        
        Notes__c noteLead = new Notes__c(Lead__c=singleLead.Id, Type__c='Lead');
        Notes__c noteAccount = new Notes__c(Account__c=singleAccount.Id);
        Notes__c noteOppty = new Notes__c(Opportunity__c=singleOppty.Id);
        
        //Lead notes
        PageReference pageRef = Page.LogDisposition;
        pageRef.getParameters().put('leadid', singleLead.Id);
        pageRef.getParameters().put('type', 'Lead');
        Test.setCurrentPageReference(pageRef);
 
        ApexPages.StandardController sc = new ApexPages.standardController(noteLead);        
        LogDispositionExtension logDispo = new LogDispositionExtension(sc);
        
        logDispo.RenderReason();
        logDispo.saveNote();
        
        //Account notes
        PageReference pageRef1 = Page.LogDisposition;
        pageRef1.getParameters().put('type', 'accnt');
        Test.setCurrentPageReference(pageRef1);
        ApexPages.StandardController sc1 = new ApexPages.standardController(noteAccount);        
        LogDispositionExtension logDispo1 = new LogDispositionExtension(sc1); 
        logDispo1.saveNote();        
        
        //Opportunity notes
        PageReference pageRef2 = Page.LogDisposition;
        pageRef2.getParameters().put('type', 'oppty');
        Test.setCurrentPageReference(pageRef2);
        ApexPages.StandardController sc2 = new ApexPages.standardController(noteOppty);        
        LogDispositionExtension logDispo2 = new LogDispositionExtension(sc2);
        logDispo2.saveNote();
        
        test.stopTest();
    }
}